package com.xyzh05.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Entity
@Table(name="department")
@Searchable
@XmlRootElement
public class Department extends BaseObject implements Serializable {
	private static final long serialVersionUID = -8746260996104211694L;
	
	public static final Long DEPARTMENT = 11L;
	public static final Long SECTION = 12L;
	
	public static final Short DEFAULT_SORTED = 0;
	public static final Short DEFAULT_STATUS = 1;
	
	private Long id;
    private Department department;
    private DictItem dictItemByIndustry;
    private DictItem dictItemByDeptTypeId;
    private User user;
    private String displayName;
    private String fullName;
    private String description;
    private String address;
    private Short sorted = DEFAULT_SORTED;
    private Short status = DEFAULT_STATUS;
    private Set<Department> departments = new HashSet<Department>(0);
	private Set<Role> roles = new HashSet<Role>(0);

    @Id @GeneratedValue(strategy=GenerationType.AUTO) @SearchableId    
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="parent_id")
    public Department getDepartment() {
        return this.department;
    }
    
    public void setDepartment(Department department) {
        this.department = department;
    }
    
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="industry")
    public DictItem getDictItemByIndustry() {
        return this.dictItemByIndustry;
    }
    
    public void setDictItemByIndustry(DictItem dictItemByIndustry) {
        this.dictItemByIndustry = dictItemByIndustry;
    }
    
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="dept_type_id", nullable=false)
    public DictItem getDictItemByDeptTypeId() {
        return this.dictItemByDeptTypeId;
    }
    
    public void setDictItemByDeptTypeId(DictItem dictItemByDeptTypeId) {
        this.dictItemByDeptTypeId = dictItemByDeptTypeId;
    }
    
    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="leader_id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
    @Column(name="display_name", nullable=false, length=32)
    @SearchableProperty
    public String getDisplayName() {
        return this.displayName;
    }
    
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    
    @Column(name="full_name", length=64)
    @SearchableProperty
    public String getFullName() {
        return this.fullName;
    }
    
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    
    @Column(name="description", length=512)
    @SearchableProperty
    public String getDescription() {
        return this.description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    @Column(name="address", length=128)
    @SearchableProperty
    public String getAddress() {
        return this.address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    @Column(name="sorted")
    @SearchableProperty
    public Short getSorted() {
        return this.sorted;
    }
    
    public void setSorted(Short sorted) {
        this.sorted = sorted;
    }
    
    @Column(name="status")
    @SearchableProperty
    public Short getStatus() {
        return this.status;
    }
    
    public void setStatus(Short status) {
        this.status = status;
    }
    
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="department")
    public Set<Department> getDepartments() {
        return this.departments;
    }
    
    public void setDepartments(Set<Department> departments) {
        this.departments = departments;
    }

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="department")
    public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Department pojo = (Department) o;

        if (department != null ? !department.equals(pojo.department) : pojo.department != null) return false;
        if (dictItemByIndustry != null ? !dictItemByIndustry.equals(pojo.dictItemByIndustry) : pojo.dictItemByIndustry != null) return false;
        if (dictItemByDeptTypeId != null ? !dictItemByDeptTypeId.equals(pojo.dictItemByDeptTypeId) : pojo.dictItemByDeptTypeId != null) return false;
        if (user != null ? !user.equals(pojo.user) : pojo.user != null) return false;
        if (displayName != null ? !displayName.equals(pojo.displayName) : pojo.displayName != null) return false;
        if (fullName != null ? !fullName.equals(pojo.fullName) : pojo.fullName != null) return false;
        if (description != null ? !description.equals(pojo.description) : pojo.description != null) return false;
        if (address != null ? !address.equals(pojo.address) : pojo.address != null) return false;
        if (sorted != null ? !sorted.equals(pojo.sorted) : pojo.sorted != null) return false;
        if (status != null ? !status.equals(pojo.status) : pojo.status != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = (department != null ? department.hashCode() : 0);
        result = 31 * result + (dictItemByIndustry != null ? dictItemByIndustry.hashCode() : 0);
        result = 31 * result + (dictItemByDeptTypeId != null ? dictItemByDeptTypeId.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        result = 31 * result + (displayName != null ? displayName.hashCode() : 0);
        result = 31 * result + (fullName != null ? fullName.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        result = 31 * result + (sorted != null ? sorted.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("department").append("='").append(getDepartment()).append("', ");
        sb.append("dictItemByIndustry").append("='").append(getDictItemByIndustry()).append("', ");
        sb.append("dictItemByDeptTypeId").append("='").append(getDictItemByDeptTypeId()).append("', ");
        sb.append("user").append("='").append(getUser()).append("', ");
        sb.append("displayName").append("='").append(getDisplayName()).append("', ");
        sb.append("fullName").append("='").append(getFullName()).append("', ");
        sb.append("description").append("='").append(getDescription()).append("', ");
        sb.append("address").append("='").append(getAddress()).append("', ");
        sb.append("sorted").append("='").append(getSorted()).append("', ");
        sb.append("status").append("='").append(getStatus()).append("', ");
        
        sb.append("]");
      
        return sb.toString();
    }

}
